home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1874 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.2 KB  |  100 lines

  1. Path: news.cs.tut.fi!usenet
  2. From: p150650@kaarne.cs.tut.fi (Tero Pulkkinen)
  3. Newsgroups: comp.lang.c++
  4. Subject: templates and virtual functions
  5. Date: 13 Jan 1996 19:44:57 +0200
  6. Organization: Tampere University of Technology, Finland
  7. Sender: p150650@kaarne.cs.tut.fi
  8. Distribution: inet
  9. Message-ID: <xdu20p4kmzp.fsf@kaarne.cs.tut.fi>
  10. NNTP-Posting-Host: kaarne.cs.tut.fi
  11. NNTP-Posting-User: p150650
  12. X-Newsreader: Gnus v5.0.13
  13.  
  14.  
  15. I have a small problem with combining runtime information (maybe user
  16. selectable) and templates. Now it seems that at the interface where
  17. your code moves from handling pointers to a base class to an template
  18. instantiation you need to list all the possible template functions
  19. you can call from the interface. This looks like a big maintainance
  20. problem, and i'd like to find workaround. Virtual member function
  21. templates would solve the problem, but as you know from D&E there's
  22. a good reason C++ doesnt support those.
  23.  
  24. so, to call a template function, you always need to supply
  25. the template parameter. Now i have a pointer to a base class of the
  26. that type, and i'd like to call a template function with the derived
  27. class as a template parameter.
  28.  
  29. The basic problem i try to solve using templates is avoiding virtual
  30. function calls in the most critical parts of the code without rewriting
  31. all algorithms for every single type i have. I.e. I have few algorithms
  32. of drawing different kind shapes pixel by pixel to the screen. Now i also
  33. need to make all the drawing functions generic, so that it works with
  34. different kind of display devices, without rewriting the drawing algorithms.
  35. The problem comes when i cant afford to make virtual function call for
  36. every pixel. So, i have the drawing algorithms defined as follows:
  37.  
  38. template <class Screen>
  39. void drawblock() {
  40. // now use Screen and Screen::iterator to draw a block to any screen
  41. // pixel by pixel.
  42. }
  43.  
  44. Then i might have at the start of the program a detection of the display
  45. device or let the user select the display device to use. That selection
  46. gives me a pointer to a some ScreenDevice-base class. But how to
  47. call those template functions without listing all the algorithms and
  48. all different screen's somewhere in the program?
  49.  
  50. The selection of the ScreenDevice is made at only one place at the start
  51. of the program. The selection must be runtime, but i still wouldnt like to
  52. have all functions virtual functions nor would i want to have all functions
  53. templates.
  54.  
  55. The sollutions i've found:
  56.  
  57. 1) Get rid of templates and use virtual functions only.
  58.    - This is not working because of I cant afford virtual function call
  59.      overhead for every pixel i draw to screen. Templates allow the
  60.      critical part to be inlined, which makes it more than 30 times
  61.      faster.
  62.  
  63. 2) Make all the functions which can call any screen handling have screen
  64.    as a template parameter. At the selection only have to call one template
  65.    function that runs the whole program. (and only have to list the
  66.    different screen devices once)
  67.    - This is not too good, because alot of code is duplicated even if its
  68.      not really necessary. Most code really doesnt draw pixels to screen,
  69.      they just call other functions that make the drawing.
  70.      (means all code is in template functions!)
  71.    - It also makes the compile times really enormous.
  72.    (btw. this is how i have it implemented right now:)
  73.  
  74. 3) The third sollution is to make some kind of interface, which stores the
  75.    selected screen device information and for every different algorithm
  76.    make a virtual function call for  different screen devices, and call te
  77.    template function from that virtual function.
  78.    - problem is that with this case i need to list all the algorithms in
  79.      every screen device i have. This is a maintainance problem and i'm
  80.      looking for better sollution. Though this is the only sollution
  81.      i've found that's good enough for runtime efficency and for
  82.      program size requirements. This is what i'm about to implement, if
  83.      i cant figure any better sollution. (This is similar sollution than
  84.      for example pascal's typecase to replace virtual functions, and it
  85.      sure doesnt sound too good to me (=list everything in million places))
  86.  
  87. So, if anyone got some way to solve the problem better, i'd like to hear
  88. them.
  89.  
  90. -- Tero Pulkkinen -- terop@kotka.cs.tut.fi --
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.